#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
 #include <unistd.h>
 #include <signal.h>
 #include<errno.h>
 #include<stdint.h>
int a;
pthread_t pid;

void * pth(void *arg)
{
    while(1)
    {
        if(a==13)
        {
             a =1;
             //printf("a = %d\n",a);
             //pthread_exit((void *)1);
             return ((void*)1);
        }    
    } 
}

void * pth2(void *arg)
{
         void *tret;
         printf("pth2 start\n");
        int ret = pthread_join(pid, &tret);//阻塞,退出返回值为0
        if (!ret) 
        {
            printf("线程终止, code=%ld\n", (long)tret);
            pthread_exit((void *)1);
        }      
}


void send_data(void *data,int len);


struct st{
    uint16_t data16[20];
    uint8_t data8[10];
};
void main() 
{
    a=0;
    struct st data;
    for(int i=0;i<20;i++)
    {
        data.data16[i] = i;
    }
    for(int i=0;i<10;i++)
    {
        data.data8[i] = i+21;
    }
    char str[20][200];
    int j=0;
    int len;
    len  = sizeof(data.data16)/sizeof(uint16_t);
    for(int i=0;i<len;i++)
    {
        sprintf(str[j++],"%d",data.data16[i]);
        //printf("%s\n",str[j]);
        //printf("%d\n",data.data16[i]);
    }

    len  = sizeof(data.data8)/sizeof(uint8_t);
    for(int i=0;i<len;i++)
    {
        sprintf(str[j++],"%d",data.data8[i]);
        //printf("%s\n",str[j]);
        //printf("%d\t",data.data8[i]);
    }
    // char *p = malloc(sizeof(mydata));
    // p =fun(20,mydata);
    //send_data(str,sizeof(str));
    //send_data(str,j);

    printf("====================\n");

   // free(p);
}

void send_data(void *data,int len)
{
    printf("len = %d\n",len);
    for(int i=0;i<len;i++)
    {
        printf("%s\n",data+200*i);
    }
    // printf("%04x\n",data);
    //打印数据
}

//================================================================线程测试
    //pthread_t pid;
    // pthread_t pid2;
    
    // int ret = pthread_create(&pid, NULL, pth, NULL);
    // if (ret) 
    // {
    //     printf("error\n");
    // }
    // ret = pthread_create(&pid2, NULL, pth2, &pid);
    // if (ret) 
    // {
    //     printf("error2\n");
    // }
    // while(1)
    // {
    //     a++;
    //     sleep(1);
    //     printf("a = %d\n",a);
    //     int kill_rc = pthread_kill(pid2,0);
    //     if(kill_rc == ESRCH)
    //         printf("the specified thread did not exists or already quit\n");//非阻塞判断线程退出
    //     else if(kill_rc == EINVAL)
    //         printf("signal is invalid\n");
    //     else
    //         printf("the specified thread is alive\n");        
    // }
//===============================================================传整形数组
    // int mydata[20];
    // for(int i=0;i<20;i++)
    // {
    //     mydata[i] = i;
    // }
    // char str[20][200];
    // for(int i=0;i<20;i++)
    // {
    //     sprintf(str[i],"%d",mydata[i]);
    //     printf("%s\n",str[i]);
    // }
    // send_data(str,sizeof(str));
    //=========================================================整形数组转字符串
    // int a[20];
    // char data[100] = {'\0'};
    // char str[100] = {'\0'};
    // for(int i=0;i<20;i++)
    // {
    //     a[i] = i;
    //     sprintf(str,"%d",a[i]);
    //     strcat(data,str);
    //     strcat(data," ");//字符串连接
    // }
    
    // printf("%d\n",strlen(data));//50
    // printf("%s\n",data);//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19,20个空格，30个数字
/*
  printf("uint8_t  size=%d\r\n",sizeof(uint8_t));
  printf("uint16_t  size=%d\r\n",sizeof(uint16_t));
  printf("uint32_t  size=%d\r\n",sizeof(uint32_t));
  printf("short int  size=%d\r\n",sizeof(short int));
  printf(" int  size=%d\r\n",sizeof(int));
  printf("long int  size=%d\r\n",sizeof(long int));

  uint8_t  size=1
  uint16_t  size=2
  uint32_t  size=4
  short int  size=2
  int  size=4
  long int  size=4 
  linux下long int 为8字节
  */